package com.spring.mao.aspect;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
public class HttpLogAspect {

    private final  static Logger logger = LoggerFactory.getLogger(HttpLogAspect.class);


    @Pointcut("execution(public * com.spring.mao.*.controller.*.*(..))")
    private void httpLog(){}

    @Before("httpLog()")
    private void httpLogBefore(JoinPoint joinPoint){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        //url
        logger.info("url={}",request.getRequestURL());
        //method
        logger.info("method={}",request.getMethod());
        //requestIp
        logger.info("requestIp={}",request.getRemoteAddr());
        //类方法
        logger.info("className={}",joinPoint.getSignature().getDeclaringTypeName()+"."+
                joinPoint.getSignature().getName());
        //参数
        logger.info("args={}",joinPoint.getArgs());


    }

}
